/**
 * org.assess.oa.system.action.IndexAction
 */
package org.assess.oa.system.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;
import org.assess.core.action.BaseAction;
import org.assess.core.bean.UserSession;
import org.assess.core.bean.node.Node;
import org.assess.core.common.Constants;
import org.assess.core.service.ServiceResult;
import org.assess.oa.system.bean.TbUser;
import org.assess.oa.system.service.TbMenuService;
import org.assess.oa.system.service.TbUserService;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;

/**
 * @description 首页/登录Action
 * @author XiaLongxiang
 * @createtime 2012-5-4 下午5:22:25
 */
public class IndexAction extends BaseAction {
    /** 序列化号 */
    private static final long serialVersionUID = -5211601013373833620L;
    
    private String username;
    
    private String password;
    
    /** 用户service */
    private TbUserService tbUserService;
    
    /** 菜单的Service */
    private TbMenuService tbMenuService;
    
    /**
     * 用户登录操作
     * @return
     */
    public String login() {
        try {
            Object[] result = this.tbUserService.login(username, password);
            
            TbUser _tempUser = (TbUser) result[1];
            if (result[0] == ServiceResult.login_success && _tempUser != null
                    && StringUtils.isNotBlank(_tempUser.getUser_code())) {
                UserSession session = (UserSession) ActionContext.getContext().getSession()
                        .get(Constants.G_USER_SESSION);
                if (session == null) {
                    session = new UserSession();
                }
                session.setLastTime(_tempUser.getLogin_time().getTime());
                session.setFullName(_tempUser.getFullName());
                session.setNickName(_tempUser.getNickName());
                session.setUserCode(_tempUser.getUser_code());
                session.setTheme(Constants.G_DEFAULT_THEME);
                ActionContext.getContext().getSession().put(Constants.G_USER_SESSION, session);
            }
            else {
                super.setMessage(super.getText(((ServiceResult) result[0]).getMessage_code()));
                return INPUT;
            }
        }
        catch (Exception ex) {
            super.setMessage(super.getText((ServiceResult.login_error_server).getMessage_code()));
            return INPUT;
        }
        
        return SUCCESS;
    }
    
    /**
     * 用户登出
     * @return
     */
    public String logout() {
        ActionContext.getContext().getSession().remove(Constants.G_USER_SESSION);
        return SUCCESS;
    }
    
    /**
     * 显示首页，首页中查询用户所拥有的菜单权限等基本信息
     * @return
     */
    public String showIndexJsp() {
        return SUCCESS;
    }
    
    /**
     * 加载用户菜单，并返回其JSON表现形式
     * @return
     */
    public String loadUserMenu() {
        UserSession session = (UserSession) ActionContext.getContext().getSession().get(Constants.G_USER_SESSION);
        List<Node> records = this.tbMenuService.queryUserMenu(session.getUserCode(), Constants.G_SYSTEM_CODE);
        Map<String, Node> map = new HashMap<String, Node>();
        this.takeNodes(records, map);
        session.setNodes(map);
        
        String jsonString = JSON.toJSONString(records);
        super.setJsonString(jsonString);
        return SUCCESS;
    }
    
    private void takeNodes(List<Node> records, Map<String, Node> map) {
        if (records != null && !records.isEmpty()) {
            for (Node node : records) {
                if ("menu".equalsIgnoreCase(node.getType())) {
                    map.put(node.getId(), node);
                    takeNodes(node.getNodes(), map);
                }
            }
        }
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * 设置用户Service层对象
     * @param tbUserService
     */
    public void setTbUserService(TbUserService tbUserService) {
        this.tbUserService = tbUserService;
    }
    
    public void setTbMenuService(TbMenuService tbMenuService) {
        this.tbMenuService = tbMenuService;
    }
}
